********************************************************************************
* Paper： The Technical Default Spread, by Emilio Bisetti, Kai Li, and Jun Yu
* This file is used to produce the emprical results in the main paper
* Authors: Emilio Bisetti, Kai Li, and Jun Yu
* Date: 2024, May 14
********************************************************************************

clear all
set more off

//ssc install freduse

///////////////////////////////////////////////////
// Figure 3 : time series of aggregate strictness
///////////////////////////////////////////////////

// First set the path to the folder of this do file

// we first get quarterly real GDP growth rate from Fed
// ssc install freduse
//freduse GDPC1,clear 
//drop date
//gen year = year(daten)
//gen qtr = quarter(daten)
//gen yearq = yq(year,qtr)
//format yearq %tq
//tsset yearq
//gen gdp = 100*(GDPC1-L1.GDPC1)/L1.GDPC1
//save Pseudodata/GDPgrowth,replace

// Then we plot the figure
use Pseudodata/strictness,clear
set scheme  s2mono  
gen strictness = strictness_5ratios*100
collapse strictness, by(yearq)

merge 1:1 yearq using Pseudodata/GDPgrowth  //GDPgrowth is computed using GDPC1 file from Fed website https://fred.stlouisfed.org/series/GDPC1
keep if _merge ==3
drop _merge
tsset yearq

graph twoway line gdp yearq,   ///
      ytitle("Real GDP Growth") ///
      lpattern(solid shortdash longdash) lwidth(medium medium medium) ||  ///
      line strictness yearq, yaxis(2) ytitle("Strictness",axis(2)) ylabel(25(5)50, axis(2))  ///	  title("Panel A: Time Series") ///
	  lpattern(dash) lwidth(thick) xtitle("", axis(1)) ||,legend(row(2) label(1 "Real GDP Growth") label(2 "Strictness"))

// output the figure	  
graph export Output\figure3.pdf, as(eps) preview(off) replace

	  

///////////////////////////////////////////////////
// Figure 4 : RDD result
///////////////////////////////////////////////////

use Pseudodata/rdd_data,clear
// We do RDD use debt_to_ebitda covenant

gen violation_debteb = .
replace violation_debteb = 1 if (Debt_to_EBITDA > max_debt_to_ebitda & Debt_to_EBITDA ~= . & max_debt_to_ebitda ~= .)
replace violation_debteb = 0 if (Debt_to_EBITDA <= max_debt_to_ebitda & Debt_to_EBITDA ~= . & max_debt_to_ebitda ~= .)

gen gap_debteb = Debt_to_EBITDA-max_debt_to_ebitda
gen dum_gap_int = violation_debteb*gap_debteb
gen gap_debteb2 = gap_debteb*gap_debteb
gen dum_gap_int2 = violation_debteb*gap_debteb2

drop if violation_debteb == .
sort gvkey yearq
bysort gvkey yearq: egen mean_exretadj = mean(exretadj)
duplicates drop gvkey yearq, force
drop exretadj
rename mean_exretadj exretadj
replace exretadj = exretadj*12

gen gap_debteb_bin=floor(gap_debteb*7)/7
bys gap_debteb_bin: egen mean_exretadj_bin = mean(exretadj)


twoway (lpolyci exretadj gap_debteb if gap_debteb>=0, bwidth(0.3) legend(off)  yline(0,lcolor(black) lwidth(thin)) xline(0, lcolor(maroon) lwidth(medthick) lpattern(longdash)) ///
xtitle("Lagged Distance to Debt/EBITDA Covenant Threshold", size(medlarge)) ytitle("Excess Return (%)", size(medlarge)) ylab(, glcol(gs14)) ciplot(rline) alpattern(shortdash) alcolor(black) clpatter(solid) clwidth(medthick) clcolor(black)) ///
||(lpolyci exretadj gap_debteb if gap_debteb<0, bwidth(0.3) ciplot(rline) alpattern(shortdash) alcolor(black) clpatter(solid) clwidth(medthick) clcolor(black)) ///
||(scatter mean_exretadj_bin gap_debteb_bin if gap_debteb_bin>0, msize(medium) mcolor(gs8%50) msymbol(smcircle_hollow)) ///
||(scatter mean_exretadj_bin gap_debteb_bin if gap_debteb_bin<0, msize(medium) mcolor(gs8%50) msymbol(smcircle_hollow) scheme(s2mono))  if abs(gap_debteb)<= 2

// output the figure
graph export Output\figure4.pdf, as(eps) preview(off) replace
